<?php
//
//  Copyright (C) 2004-2006  Autodesk, Inc.
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of version 2.1 of the GNU Lesser
//  General Public License as published by the Free Software Foundation.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
require_once("config.php");
require_once("utils.php");
require_once(ROOT_DIR_CLASES.'/cCliente.php'); 
require_once(ROOT_DIR_CLASES.'/cClienteBykom.php'); 
require_once(ROOT_DIR_CLASES.'/MisZonas.php');
require_once 'JSON.php';

class QueryMisZonas
{
	private $args = null;

	function __construct($args)
	{
		$this->args = $args;
	}
	
	function Execute()
	{
		$result 	= array();
		$cuenta 	= $this->args['CUENTA'];
		$esflota 	= $this->args['ESFLOTA'];
		$iTablaId 	= $this->args['TABLAID'];
		$iObjetivo 	= $this->args['OBJETIVO'];
		$bIsBykom 	= $this->args['ISBYKOM'];
        //--------------------------------------	
   		if($iObjetivo==4){
   			$cuenta = $iTablaId;
   		}
   		if ($bIsBykom){
   			$zonasRegistradas = BykomCliente_byk::Zonas_Cliente($cuenta, $iObjetivo);
   		}else{
   			$zonasRegistradas = BykomCliente::Zonas_Cliente($cuenta, $iObjetivo);
   		}
        for($i = 0;  $i < $zonasRegistradas->cantidad() ; $i++ ) {
			$miZona = new MisZonas();
			if ($bIsBykom){
				$puntosPorZona = BykomCliente_byk::Puntos_Zona($zonasRegistradas->campo('ORDER_ID',$i));
			}else{
				$puntosPorZona = BykomCliente::Puntos_Zona($zonasRegistradas->campo('ORDER_ID',$i));
			}
			
			$miZona->id 	= $zonasRegistradas->campo('ORDER_ID',$i);
			$miZona->nombre = $zonasRegistradas->campo('NOMBRE',$i);
			$colorexa 		= dechex($zonasRegistradas->campo('COLOR',$i)); //convertimos el color de Decimal a Hexa
			$colorexa 		= '000000'.$colorexa;
			$colorexa 		= substr($colorexa, strlen($colorexa)-6);
			$miZona->color 	= $colorexa;			
			$miZona->datos 	= $zonasRegistradas->campo('DATOS',$i);
			$miZona->flotaId= $zonasRegistradas->campo('FLOTA_ID',$i);
			$miZona->publico= $zonasRegistradas->campo('PUBLICO',$i);
			$miZona->esflota= $zonasRegistradas->campo('ESFLOTA',$i);
			
       		if($bIsBykom){
				$miZona->tolerancia = $zonasRegistradas->campo('TOLERANCIA',$i);
			}
			for ($j = 0; $j < $puntosPorZona->cantidad(); $j++){
				$miZona->putPoint($puntosPorZona->campo('LATITUD',$j),$puntosPorZona->campo('LONGITUD',$j));
			}
        	array_push($result, $miZona);
		}
		return $result;
	}
}
?>